var bg = new Image()
bg.src = './images/starry.jpeg'
var bg2 = new Image()
bg2.src = './images/hero1.jpg'
var bg3 = new Image()
bg3.src = './images/A1-1.png'
var bg4 = new Image()
bg4.src = './images/bullet.png'
var background = {
	x: 0,
	y: 0,
	img: bg,
	speed: 5,
	//move 移动  专门存放和background坐标改变 相关的代码
	move: function () {
		this.y += this.speed;
		//随着画布不断往下移动，当背景图即将要离开画布时，我们巴背景的位置回归到初始位置
		if (this.y > HEIGHT) {
			this.y = 0
		}
	},
	//  间隔定时器来实现 动画
	draw: function (ctx) {
		ctx.drawImage(
			this.img,
			this.x,
			this.y,
			WIDTH,
			HEIGHT
		);

		//绘制背景

		ctx.drawImage(
			this.img,
			this.x,
			this.y - HEIGHT,
			WIDTH,
			HEIGHT
		);
	}


}

//player玩家对象
var player = {
	x: 0,
	y: 0,
	width: PLAYER_WIDTH,
	height: PLAYER_HEIGHT,
	img: bg2,
	isLiving:false,
	draw: function (ctx) {
		ctx.drawImage(
			this.img,
			this.x,
			this.y,
			this.width,
			this.height,
		)
	},
	//初始化  通常只被执行一次
	init: function (canvas) {
		canvas.onmousemove = function (evt) {
			//事件处理函数this指向是  事件源
			//console,log(this)
			player.x = evt.offsetX - player.width / 2
			player.y = evt.offsetY - player.height / 2

			//边缘检查
			if (player.x < 0) {
				player.x = 0;
			}
			if (player.x > WIDTH - player.width) {
				player.x = WIDTH - player.width;
			}
			if (player.y < 0) {
				player.y = 0;
			}
			if (player.y > WIDTH + player.height) {
				player.y = WIDTH + player.height;
			}
		}
	}
}

var bool = [{A:false},{D:false},{W:false},{S:false}]

document.onkeydown=function(evt){
	if (evt.code==='KeyA') {
		bool.A=true ;
	}
	if(evt.code==='KeyD'){
		bool.D=true;
	}
	if(evt.code==='KeyW'){
		bool.W=true;
	}
	if(evt.code==='KeyS'){
		bool.S=true;
	}
}
document.onkeyup=function(evt){
	if (evt.code==='KeyA') {
		bool.A=false;
	}
	if(evt.code==='KeyD'){
		bool.D=false;
	}
	if(evt.code==='KeyW'){
		bool.W=false;
	}
	if(evt.code==='KeyS'){
		bool.S=false;
	}
}
setInterval(function(){
	if (bool.A) {
		player.x -=10
	}
	if (bool.D) {
		player.x +=10
	}
	if (bool.W) {
		player.y -=10
	}
	if (bool.S) {
		player.y +=10
	}
	if (player.x < 0) {
		player.x = 0;
	}
	if (player.x > WIDTH - player.width) {
		player.x = WIDTH - player.width;
	}
	if (player.y < 0) {
		player.y = 0;
	}
	if (player.y > WIDTH + player.height) {
		player.y = WIDTH + player.height;
	}
},20)
//敌机构造函数
function Player1(x,y){
	this.x = x;
	this.y = y;
	this.width = PLAYER2_WIDTH;
	this.height = PLAYER2_HEIGHT;
	this.speed  = PLAYER2_SPEED;
	this.img = bg3;
	//敌机移动
	this.move = function(player1List){
		this.y +=this.speed;
	    if (this.y > HEIGHT + 100) {
			//敌机数组回收
			player1List.recycle(this)
		}
	};
	this.draw = function (ctx) {
		ctx.drawImage(
			this.img,
			this.x,
			this.y,
			this.width,
			this.height,
		)
	}
	//能够让敌机在生成之后  随机的1-3秒钟之间，速度变成初速度的1-3倍
	//用到延时定时器  Timeout

	this.timer = setTimeout(() => {
		this.speed = this.speed * getRandomInt(1,3)
	},1000 * getRandomInt(1,3))
}

// 构造函数生成子弹对象
function Bullet(x,y,width,height,img,speed){
	this.x = x;
	this.y = y;
	this.width = width
	this.height = height
	this.img  = img
	this.speed = speed



	this.move = function(bulletList){
		this.y -= this.speed
		if (this.y < -this.height) {
			//子弹数组回收
			bulletList.recycle(this)
		}
	}
	this.draw = function(ctx){
		ctx.drawImage(this.img,this.x,this.y,this.width,this.height)
	}
}

//用于生成  管理对象的容器
function  ObjList(){
	this.data = []
	this.recycle = function(obj){
		var index = this.data.findIndex(function(item){
			return item ===obj
		})
		if(index  !== -1){
			this.data.splice(index,1)
		}
	}
	this.clear = function() {
		this.data = []
	  }
}

function getRandomInt(objA,objB){
var canterA = getCenter(objA)
}

var explosionImg = []
for(var i = 1;i<=19;i++){
	var img = new Image()
	img.src =`./images/explosion${i}.png`
	explosionImg.push(img)
}

function Explosion(x,y,width,height){
	this.x = x
	this.y = y
	this.width = width
	this.height =height
	this.imgList = explosionImg  //爆炸对象的图片数组
	this.count = 0//计数  计算当前应该绘制  那一张爆炸的图片
	this.num = 0//总计数
//绘制爆炸
	this.draw = function(ctx){
		//console.log(this.imgList[this.count])
		//把this.count中的数 作为  图片数组的索引 来决定在画布上绘制那一张图
		ctx.drawImage(this.imgList[this.count],this.x,this.y,this.width,this.height)
	}
	// 在move方法中 我们改变count从而来控制  画布上绘制出动态的爆炸效果+
	this.move = function(explosionList){
        this.num ++

		if(this.num<18){
			this.count ++
		}
		else(
			this.count > 0 && this.count --
		)

		if(this.num >37){
			// this.num = 0
			// this.count = 0
			explosionList.recycle(this)
		}
	}
}
//  boss的构造函数
var bossImg = new Image();
bossImg.src = "./images/boss1.jpeg";

function Boss() {
  this.x = WIDTH / 2 - 125;
  this.y = -250;
  this.width = 250;
  this.height = 250;
  this.img = bossImg;
  this.HP = 2000
  this.show = false
  this.movingRight = true;  // 决定boss是左移还是右移的一个参数

  this.move = function () {
    // boss出场后 先向下移动
    if (this.y < 50) {
      this.y += 1;
    } else {
      // 然后再左右移动
      if (this.movingRight) {
        this.x += 3;
        //  右边到头了就左移
        if (this.x >= WIDTH - this.width) {
          this.movingRight = false;
        }
      } else {
        this.x -= 3;
        // 左边到头了就右移
        if (this.x <= 0) {
          this.movingRight = true;
        }
      }
    }
  };
  this.draw = function (ctx) {
    ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
  };

  //  boss发射子弹的方法
  this.fire = function (player1BulletList) {
    //  一次发射3颗子弹 子弹是不同的方向
    player1BulletList.data.push(new Player1Bullet(this.x + this.width/2 - 7, this.y + this.height, DOWN))
    player1BulletList.data.push(new Player1Bullet(this.x + this.width/2 - 7, this.y + this.height, LEFT))
    player1BulletList.data.push(new Player1Bullet(this.x + this.width/2 - 7, this.y + this.height, RIGHT))
  }

  // boss 在快挂的时候的效果
  this.dying = function(explosionList) {
    for(var i = 0; i < 5; i++) {
      explosionList.data.push(new Explosion( getRandomInt(this.x, this.x + this.width ),
      getRandomInt(this.y, this.y + this.height),100,100))
    }
  }
}


var player1BulletImg = new Image()
player1BulletImg.src = './images/mami.png'


function Player1Bullet(x, y, direction) {
	this.x = x
	this.y = y
	this.width = 15
	this.height = 15
	this.img = player1BulletImg
	this.direction =  direction // 子弹飞行的方向
	this.speed = 5
  
	this.move = function(player1BulletList) {
	  if(this.direction === DOWN) {
		this.y += this.speed
	  }
	  if(this.direction === LEFT) {
		this.y += this.speed / 1.2
		this.x -= this.speed / 1.5
	  }
	  if(this.direction === RIGHT) {
		this.y += this.speed / 1.2
		this.x += this.speed / 1.5
	  }
  
	  //  飞出画布  回收子弹
	  if(this.y > HEIGHT || this.x < -this.width || this.x > WIDTH) {
		player1BulletList.recycle(this)
	  }
	}
  
  
	this.draw = function (ctx) {
	  ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
	};
  
  }